﻿//
//  Monkey Manager
//  Copyright (C) 2012 Victor Tereschenko
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//     
// ========================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

using ManagerDB;

namespace ManagerDB.Options
{
    /// <summary>
    /// Helps to create options.
    /// </summary>
    public class OptionsFactory
    {
        /// <summary>
        /// Creates new options.
        /// </summary>
        /// <returns></returns>
        public static ManagerOptions Create()
        {
            var options = new ManagerOptions();
            options.DatabaseType = ManagerOptions.DBType.SQLite;
            options.DBFile = "video.db";
            options.DBName = "<To watch>";
            options.OptionsFilename = "database.xml";
            return options;
        }

        /// <summary>
        /// Tries to load options from xml file. May return null.
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static ManagerOptions ReadFromFile(string fileName)
        {
            if (!System.IO.File.Exists(fileName))
                return null;

            XmlDocument doc = new XmlDocument();
            try
            {
                doc.Load(fileName);
            }
            catch (Exception)
            {
                return null;
            }

            var options = new ManagerOptions();
            options.OptionsFilename = fileName;
            XmlNode rootNode = doc.SelectSingleNode("/database");
            XmlNode triggersNode = doc.SelectSingleNode("database/triggers");
            if (triggersNode != null)
            {
                foreach (XmlNode triggerNode in triggersNode.ChildNodes)
                {
                    XmlNodeList addTagNodes = triggerNode.SelectNodes("addTag");
                    AutoTag tag = new AutoTag();
                    tag.Pattern = triggerNode.Attributes["pattern"].Value;
                    foreach (XmlNode addTagNode in addTagNodes)
                        tag.Tags.Add(addTagNode.Attributes["tag"].Value);
                    options.AutoTags.Add(tag);
                }
            }

            if (rootNode.Attributes["BackupFile"] != null)
                options.BackupFile = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, rootNode.Attributes["BackupFile"].Value);

            options.DBFile = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, rootNode.Attributes["DBFile"].Value);
            options.DBName = rootNode.Attributes["DBName"] != null ? rootNode.Attributes["DBName"].Value : "<To watch>";
            options.DatabaseType = readDBType(rootNode);
            // TODO: read replacements here!
            if (rootNode.Attributes["ReplacementsFile"] != null)
                options.ReplacementsFile = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, rootNode.Attributes["ReplacementsFile"].Value);
            else
                options.ReplacementsFile = "";

            return options;
        }

        /// <summary>
        /// Tries to parse DBType field. If there is no such a field then the default values will be returned (SQLite).
        /// </summary>
        /// <exception cref="Exception">In case of incorrect DBType.</exception>
        /// <param name="node"></param>
        /// <returns></returns>
        private static ManagerOptions.DBType readDBType(XmlNode node)
        {
            if (node.Attributes["DBType"] == null)
                return ManagerOptions.DBType.SQLite;

            switch (node.Attributes["DBType"].Value)
            {
                case "SQLite":
                    return ManagerOptions.DBType.SQLite;
                case "Folder":
                    return ManagerOptions.DBType.Folder;
            }
            throw new Exception("Unknown DB type: " + node.Attributes["DBType"].Value);
        }
    }
}
